﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KOPLibrary.PreferenceManagement;
using Microsoft.Office.Tools.Ribbon;
using System.IO;
using System.Resources;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using KOPLibrary.Exceptions;
using KOPLibrary.UpdateService;

namespace KOPLibrary.TemplateManagement
{
    public enum TemplateType
    {
        Word,
        Excel
    }

    public class TemplateManager
    {
        public TemplateType Type { get; private set; }
        public string Directory { get; private set; }

        public static string Extension(TemplateType type)
        {
            if (type == TemplateType.Excel)
                return "xltx";
            else return "dotx";
        }

        public TemplateManager(TemplateType type, string initialDirectory)
        {
            this.Directory = initialDirectory;
            this.Type = type;
        }

        public void AddToRecentlyUsedList(string selectedTemplate)
        {
            List<string> current = GetRecentlyUsedList();
            int existingIndex = current.IndexOf(selectedTemplate);
            if (existingIndex < 0)
                current.Insert(0, selectedTemplate);
            else if (existingIndex != 0)
            {
                current.RemoveAt(existingIndex);
                current.Insert(0, selectedTemplate);
            }
            Dictionary<string, string> valueList = new Dictionary<string, string>(current.Count);
            for (int i = 0; i < current.Count; i++)
            {
                if (!File.Exists(current[i])) continue;
                valueList.Add(string.Format("r{0}", i), current[i]);
                if (i > 11)
                    break;
            }
            PreferenceManager.SaveGroupValues(@"RecentTemplates\" + Type.ToString(), valueList);
        }

        public List<string> GetRecentlyUsedList()
        {
            return PreferenceManager.GetGroupValues(@"RecentTemplates\" + Type.ToString());
        }

        public void RefreshGalleryItems(global::Microsoft.Office.Tools.Ribbon.RibbonGallery gallery)
        {
            gallery.Items.Clear();
            List<string> currentItems = GetRecentlyUsedList();
            ResourceManager rmanager = new ResourceManager("KOPLibrary.ImageResource", Assembly.GetExecutingAssembly());
            foreach (var item in currentItems)
            {
                if (!File.Exists(item.ToString())) continue;
                Microsoft.Office.Tools.Ribbon.RibbonDropDownItem control = new RibbonDropDownItem();
                control.Label = Path.GetFileNameWithoutExtension(item);
                control.Tag = item;
                control.Image = (Image)rmanager.GetObject(Type.ToString());
                control.ScreenTip = Type.ToString() + " Şablonunu Kullan";
                control.SuperTip = control.Label + " şablonunu kullanarak yeni bir belge oluşturur.";
                gallery.Items.Add(control);
            }
        }


    }
}
